<template>
  <el-menu default-active="1" :collapse="isCollapse" class="el-menu-vertical-demo" background-color="#545c64" text-color="#fff" active-text-color="#ffd04b">
    <h3 v-show="!isCollapse">小弟管理系统</h3>
    <h3 v-show="isCollapse">小弟</h3>
    <el-menu-item :index="menu.path" v-for="menu in noChildren" :key="menu.path" @click="clickMenu(menu)">
      <i :class="`el-icon-` + menu.icon"></i>
      <span slot="title">{{ menu.label }}</span>
    </el-menu-item>
    <el-submenu index="1" v-for="(item, index) in hasChildren" :key="index">
      <template slot="title">
        <i :class="`el-icon-` + item.icon"></i>
        <span>{{ item.label }}</span>
      </template>
      <el-menu-item :index="subItem.path" v-for="(subItem, subIndex) in item.children" :key="subIndex" @click="clickMenu(subItem)">
        <i :class="`el-icon-` + subItem.icon"></i>
        <span>{{ subItem.label }}</span>
      </el-menu-item>
    </el-submenu>
  </el-menu>
</template>

<script>
import { mapState } from 'vuex'
export default {
  computed: {
    hasChildren() {
      return this.menus.filter((item) => item.children)
    },
    noChildren() {
      return this.menus.filter((item) => !item.children)
    },
    ...mapState({
      isCollapse: (state) => state.tab.isCollapse,
      menus: (state) => state.tab.menus,
    }),
  },
  data() {
    return {
      asideMenus: [
        { path: '/', label: '首页', icon: 's-home', name: 'home' },
        { path: '/video', label: '视频管理', icon: 'video-play', name: 'video' },
        { path: '/user', label: '用户管理', icon: 'user', name: 'user' },
        {
          path: '/other',
          label: '其他',
          icon: 'setting',
          name: 'other',
          type: 'danger',
          children: [
            { path: '/page1', label: '页面1', name: 'page1' },
            { path: '/page2', label: '页面2', name: 'page2' },
          ],
        },
      ],
    }
  },
  methods: {
    clickMenu(val) {
      if (this.$router.currentRoute.fullPath !== val.path) {
        this.$router.push({ name: val.name })
      }
      this.$store.commit('selectMenu', val)
    },
  },
}
</script>

<style lang="scss" scoped>
.el-menu {
  border: none;
  height: 100%;
  // line-height: 48px;
  // text-align: center;
  h3 {
    color: #fff;
    text-align: center;
    line-height: 48px;
  }
}

.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}
</style>
